<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>首页</title>
    @include('common-css')
    <style>

        .input_box {
            width: 100%;
            height: 40px;
            margin-top: 15px;
            margin-bottom: 15px;
            display: flex;
            justify-content: flex-start;
            align-items: center;
        }

        .input_label {
            width: 120px;
            box-sizing: border-box;
        }

        .project-table {
            margin-top: 15px;
        }

        .status-tag {
            color: #1890ff;
            border: 1px solid #91d5ff;
            border-radius: 3px;
            background: #e6f7ff;
            font-size: 12px;
            padding: 2px 3px;
        }

        .ant-table-tbody > tr > td, .ant-table-thead > tr > th {
            vertical-align: middle;
            line-height: 25px;
        }

        .search_box {
            width: 1000px;
        }

        .search_box input {
            width: 300px;
        }

        .search_box button {
            margin-left: 20px;
        }

        .search_box .ant-select {
            margin-left: 20px;
        }
    </style>
</head>
<body>

<div id="app"></div>

<script id="appTemplate" type="x-template">
    <a-locale-provider :locale="antdLocale">
        <div class="container">

            @include('admin.header-tpl')

            <main>
                <div class="common_container">

                    @include('admin.aside-tpl')
                </div>
            </main>


        </div>
    </a-locale-provider>
</script>

@include('admin.excel-export-js')
@include('common-js')

@include('admin.header-js')
@include('admin.aside-js')

<script>
    var queryParams = parseUrl(location.href).params;
    var awardsInfo = {!! json_encode(array_values(config('vars.awards')), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) !!};

</script>

<script>
    var app = new Vue({
        el: '#app',
        template: '#appTemplate',
        data: {
            profCategoryFilter: '',
            searchKeyword: '',

            pageData: [],
            loadingPageData: false,

            dialogRecallReasonVisible: false,
            recallingProject: {},
            recallReason: '',

            atPhaseFormalReview: false,

            queryParams,
            awardsInfo,
            profCategories,

            asideSelectedKeys: [queryParams.aside_key],
            antdLocale: antd.locales.zh_CN,
            ...AdminHeader.data,
            ...AdminAside.data,
            ...ExcelExport.data
        },
        computed: {
            filteredProjects() {
                var projects = this.searchFilteredProjects;
                if (this.profCategoryFilter) {
                    projects = projects.filter(project => {
                        return project.prof_category == this.profCategoryFilter
                    })
                }
                var num = 1;
                projects.forEach(project => {
                    project.num = num++
                });
                return projects
            },
            searchFilteredProjects() {
                var projects;
                if (this.searchKeyword) {
                    var q = this.searchKeyword.trim();
                    projects = this.pageData.filter(project => {
                        var text = project.project_name + project.applicant_name + project.applicant_org_name + project.applicant_tel + project.applicant_email;
                        return text.toLowerCase().indexOf(q) != -1
                    })
                } else {
                    projects = this.pageData
                }
                return projects
            },
        },
        beforeMount() {
            this.loadAsideDefaultOpenKeys()
        },
        mounted() {
            this.reloadPageData();
            this.reloadAsideData()
        },
        methods: {
            endPhaseFormalReview() {
                axios.post('/api/admin/end-phase-formal-review').then(response => {
                    if (response.data.ok) {
                        success('操作成功');
                        this.atPhaseFormalReview = false
                    } else {
                        error('操作失败')
                    }
                })
            },
            countProjectsByProfCategory(category) {
                var projects = this.searchFilteredProjects;
                if (category == '') {
                    return projects.length
                }
                var count = 0;
                projects.forEach(project => {
                    if (project.prof_category == category) {
                        count += 1
                    }
                });
                return count
            },
            handleProfCategoryChange(record, value) {
                console.log('handleProfCategoryChange', record, value);
                axios.post('/api/admin/update-project-attrs', {
                    project_id: record.id,
                    attrs: {
                        prof_category: value
                    }
                }).then(response => {
                    success('修改成功')
                })
            },
            reloadPageData1() {
                this.loadingPageData = true;
                axios.get('/api/admin/get-projects', {
                    params: {
                        award_id: queryParams.award_id,
                        status_ids: queryParams.status_ids,
                        large_group_id: queryParams.large_group_id,
                    }
                }).then(response => {
                    this.pageData = response.data;
                    this.loadingPageData = false
                })
            },
            reloadPageData() {
                this.loadingPageData = true;
                axios.get('/api/admin/get-list-projects-page-data', {
                    params: {
                        award_id: queryParams.award_id,
                        status_ids: queryParams.status_ids,
                        large_group_id: queryParams.large_group_id,
                    }
                }).then(response => {
                    if (response.data.ok) {
                        this.atPhaseFormalReview = response.data.atPhaseFormalReview;
                        this.pageData = response.data.projects
                    } else {
                        error('加载页面数据失败: ' + response.data.msg)
                    }
                    this.loadingPageData = false
                })
            },
            showRecallReasonDialog(project) {
                this.recallingProject = project;
                this.recallReason = '';
                this.dialogRecallReasonVisible = true
            },
            handleRecallReasonDialogOk() {
                if (this.recallReason == '') {
                    warn('退回原因不能为空');
                    return
                }
                this.updateProjectStatus(this.recallingProject, 'B1.2', this.recallReason);
                this.dialogRecallReasonVisible = false
            },
            updateProjectStatus(project, statusId, statusRemarks) {
                console.log('updateProjectStatus', project, statusId, statusRemarks);
                loading('正在处理，请稍候');
                var postData = {
                    project_id: project.id,
                    status_id: statusId,
                };
                if (statusRemarks) {
                    postData.status_remarks = statusRemarks
                }
                axios.post('/api/admin/update-project-status', postData)
                    .then(response => {
                        dismiss();
                        success('操作成功');
                        this.reloadPageData();
                        this.reloadAsideData()
                    })
            },
            projectFormalReviewPassed(projectId) {
                loading('正在处理，请稍候');
                axios.post('/api/admin/project-formal-review-passed', {project_id: projectId})
                    .then(response => {
                        dismiss();
                        if (response.data.ok) {
                            success('操作成功');
                            this.reloadPageData();
                            this.reloadAsideData()
                        } else {
                            error('操作失败: ' + response.data.msg)
                        }
                    })
            },
            findAwardInfoById(id) {
                return this.awardsInfo.filter(item => {
                    return item.id == id
                })[0]
            },
            ...AdminHeader.methods,
            ...AdminAside.methods,
            ...ExcelExport.methods
        }
    })
</script>

</body>
</html>